package com.vanzo.talkie;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Switch;
import android.widget.TextView;

import com.vanzo.talkie2.*;

/**
 * Created by tisen on 2018/2/5.
 */

public class LoadActivity extends BaseActivity implements View.OnClickListener {

    private TextView masterText;
    private TextView followerText;
    private SharedPreferences sharedPreferences;
    private static final String USER_MODE = "user_mode";

    private static final int MASTER_MODE = 1;
    private static final int FOLLOWER_MODE = 2;
    public static final String ACTION_AS_MASTER = "action.as.master";
    public static final String ACTION_AS_FOLLOWER = "action.as.follower";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sharedPreferences = getSharedPreferences("loading", MODE_PRIVATE);
        switchMode();
    }

    private void switchMode() {
        switch (sharedPreferences.getInt(USER_MODE, 0)) {
            case 0:
                initView();
                break;
            case MASTER_MODE:
                asMaster();
                break;
            case FOLLOWER_MODE:
                asFollower();
                break;
        }
    }

    private void initView() {
        setContentView(R.layout.activity_loading);
        masterText = (TextView) findViewById(R.id.loading_master);
        followerText = (TextView) findViewById(R.id.loading_follower);
        masterText.setOnClickListener(this);
        followerText.setOnClickListener(this);
    }

    private void asMaster() {
        Intent intent = new Intent();
        intent.setAction(ACTION_AS_MASTER);
        intent.setClass(this, TalkieActivity.class);
        startActivity(intent);
        finish();
    }

    private void asFollower() {
        Intent intent = new Intent();
        intent.setAction(ACTION_AS_FOLLOWER);
        intent.setClass(this, TalkieActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.loading_master:
                sharedPreferences.edit().putInt(USER_MODE, MASTER_MODE).apply();
                asMaster();
                break;
            case R.id.loading_follower:
                sharedPreferences.edit().putInt(USER_MODE, FOLLOWER_MODE).apply();
                asFollower();
                break;
            default:
                break;
        }
    }
}
